package edu.yourl.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
public class Gossip implements Storeable {

	private List<Gossiper> to;
	private String message;
	private Date date;
	private User author;
	private AbuseReport abuseReport = null;

	public Gossip(User author, String message) {
		this.message = message;
		this.date = new Date();
		this.author = author;
		this.to = new ArrayList<Gossiper>();
		extractUsers();
		extractUrls();
	}

	private void extractUsers() {
		for (User user : User.extractUsers(message)) {
			this.to.add(user);
			user.addGossip(this);
		}
	}
	
	private void extractUrls() {
		for (Url url : Url.extractFromText(message)) {
			to.add(url);
			url.addGossip(this);
		};
	}

	public String getMessage() {
		return message;
	}

	public Date getDate() {
		return date;
	}

	public List<Gossiper> getTo() {
		return to != null ? Collections.unmodifiableList(to) : null;
	}

	public User getAuthor() {
		return author;
	}

	public void reportAbuse(User reporter) {
		abuseReport = new AbuseReport(reporter);
	}
	
	public AbuseReport getAbuseReport() {
		return abuseReport;
	}

	public boolean isAbuse() {
		return abuseReport != null;
	}
}
